//
//  CustomDetailCell.m
//  IceBreaker
//
//  Created by Chiao Wolfire on 12-10-9.
//  Copyright (c) 2012年 NDS. All rights reserved.
//

#import "CustomDetailCell.h"
#import <QuartzCore/CALayer.h>

#define SELF_WIDTH 320.0f
#define GAP_WIDTH 8.0f
#define EVENT_IMG_WIDTH 306.0f
#define EVENT_IMG_HEIGHT 170.0f
#define HEAD_IMG_SIDE 50.0f
#define ICON_SIDE 12.0f

@implementation CustomDetailCell

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

-(id) initWithStyle: (UITableViewCellStyle)style 
    reuseIdentifier: (NSString *)reuseIdentifier 
            headImg: (UIImage *)headIcon 
               name: (NSString *)name 
             gender: (int) gender
                age: (int)age 
          eventType: (NSString *)type 
           distance: (float)distance 
         eventTitle: (NSString *)title 
          eventImgs: (NSArray *) eventImgArr 
        requirement: (NSString *)requirement 
       commentHeads: (NSArray *) commentHeadArr 
        commentTxts: (NSArray *)commentTxtArr {
    self = [super initWithStyle: style 
                reuseIdentifier: reuseIdentifier];
    if (self) {
        float currentY = GAP_WIDTH;
        // init the image and set it's coordination
        UIImageView *headImg = [[UIImageView alloc] initWithFrame: CGRectMake(GAP_WIDTH, currentY, HEAD_IMG_SIDE, HEAD_IMG_SIDE)];
        // set the image to show
        headImg.image = headIcon;
        headImg.layer.masksToBounds = YES;
        headImg.layer.cornerRadius = 5.0;
        // add the view to the cell
        [self addSubview: headImg];
        [headImg release];
        currentY += HEAD_IMG_SIDE;
        
        UILabel *nameLabel = [[UILabel alloc] initWithFrame: CGRectMake(currentY + GAP_WIDTH, GAP_WIDTH, 254, 36)];
        nameLabel.text = name;
        nameLabel.font = [UIFont systemFontOfSize: 12];
        nameLabel.numberOfLines = 0;
        [self addSubview: nameLabel];
        [nameLabel release];
        
        UIImageView *genderImg = [[UIImageView alloc] initWithFrame: CGRectMake(59, 45, ICON_SIDE, ICON_SIDE)];
        if (gender == 1) {
            genderImg.image = [UIImage imageNamed: @"male.png"];
        } else {
            genderImg.image = [UIImage imageNamed: @"female.png"];
        }
        [self addSubview: genderImg];
        [genderImg release];
        
        UIFont *font = [UIFont systemFontOfSize: 10.0f];
        
        UILabel *ageTitle = [[UILabel alloc] initWithFrame: CGRectMake(73, 45, 28, ICON_SIDE)];
        ageTitle.font = font;
        ageTitle.text = [NSString stringWithFormat: @"%d", age];
        [self addSubview: ageTitle];
        [ageTitle release];
        
        UILabel *typeTitle = [[UILabel alloc] initWithFrame: CGRectMake(187, 45, 60, ICON_SIDE)];
        typeTitle.font = font;
        typeTitle.textAlignment = UITextAlignmentRight;
        typeTitle.text = type;
        [self addSubview: typeTitle];
        [typeTitle release];
        
        UIImageView *distanceImg = [[UIImageView alloc] initWithFrame: CGRectMake(249, 45, ICON_SIDE, ICON_SIDE)];
        distanceImg.image = [UIImage imageNamed: @"marker.png"];
        [self addSubview: distanceImg];
        [distanceImg release];
        
        UILabel *distanceTitle = [[UILabel alloc] initWithFrame: CGRectMake(263, 45, 50, ICON_SIDE)];
        distanceTitle.font = font;
        distanceTitle.textAlignment = UITextAlignmentLeft;
        distanceTitle.text = [NSString stringWithFormat: @"%0.1fkm", distance];
        [self addSubview: distanceTitle];
        [distanceTitle release];
        
        if (title && [title length] > 0) {
            currentY += GAP_WIDTH;
            UILabel *eventTitle = [[UILabel alloc] initWithFrame: CGRectMake(GAP_WIDTH, currentY, 306, 64)];
            eventTitle.text = title;
            // define a font for use
            UIFont *titleFont = [UIFont systemFontOfSize: 22];
            // make the label can contain multiple lines
            [eventTitle setNumberOfLines: 0];
            // make the label wrape the text
            eventTitle.lineBreakMode = UILineBreakModeWordWrap;
            // set the font            
            eventTitle.font = titleFont;
            // reset the frame
            eventTitle.frame = CGRectMake(GAP_WIDTH, currentY, eventTitle.frame.size.width, eventTitle.frame.size.height);
            // make the size fit the text
            [eventTitle sizeToFit];
            [self addSubview: eventTitle];
            currentY += eventTitle.frame.size.height;
            [eventTitle release];
        }

        for (UIImage *img in eventImgArr) {
            currentY += GAP_WIDTH;
            UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(GAP_WIDTH, currentY, EVENT_IMG_WIDTH, EVENT_IMG_HEIGHT)];
            imgView.image = img;
            [self addSubview: imgView];
            currentY += EVENT_IMG_HEIGHT;
            [imgView release];
        }
        
        if (requirement && [requirement length] > 0) {
            currentY += GAP_WIDTH;
            UILabel *requireTitle = [[UILabel alloc] initWithFrame: CGRectMake(GAP_WIDTH, currentY, 306, 64)];
            requireTitle.text = requirement;
            UIFont *requireFont = [UIFont systemFontOfSize: 12];
            [requireTitle setNumberOfLines: 0];
            requireTitle.lineBreakMode = UILineBreakModeWordWrap;
            requireTitle.font = requireFont;
            requireTitle.frame = CGRectMake(GAP_WIDTH, currentY, requireTitle.frame.size.width, requireTitle.frame.size.height);
            [requireTitle sizeToFit];
            [self addSubview: requireTitle];
            currentY += requireTitle.frame.size.height;
            [requireTitle release];
        }
        
        for (int i = 0; i < [commentHeadArr count]; i ++) {
            currentY += GAP_WIDTH;
            UIImageView *headView = [[UIImageView alloc] initWithFrame:CGRectMake(GAP_WIDTH, currentY, HEAD_IMG_SIDE, HEAD_IMG_SIDE)];
            headView.image = (UIImage *)[commentHeadArr objectAtIndex: i];
            headView.layer.masksToBounds = YES;
            headView.layer.cornerRadius = 5.0;
            [self addSubview: headView];
            [headView release];
            UILabel *commLabel = [[UILabel alloc] initWithFrame: CGRectMake(59, currentY, 254, HEAD_IMG_SIDE)];
            commLabel.text = (NSString *)[commentTxtArr objectAtIndex: i];
            commLabel.font = [UIFont systemFontOfSize: 12];
            commLabel.numberOfLines = 0;
            [self addSubview: commLabel];
            currentY += HEAD_IMG_SIDE;
            [commLabel release];
        }        
        self.frame = CGRectMake(0, 0, SELF_WIDTH, currentY + 2);        
        
    }
    return self;
}



-(void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
